home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / Sound.au3 < prev    next >
Encoding:
Text File  |  2007-09-08  |  10.5 KB  |  290 lines

  1. ;===============================================================================
  2. ;
  3. ; Function Name:   _SoundOpen
  4. ; Description::    Opens a sound file for use with other _Sound functions
  5. ; Parameter(s):    $hFile - The sound file, $sAlias[optianal] - a name such as sound1,
  6. ;                   if you do not specify one it is randomly generated
  7. ; Requirement(s):  AutoIt 3.2 ++
  8. ; Return Value(s): string(the sound id) - Success, 0 - Failure
  9. ;                   @extended <> 0 - open failed, @error = 2 - File doesn't exist,
  10. ;                   @error = 3 - alias contains whitespace
  11. ; Author(s):       RazerM
  12. ;
  13. ;===============================================================================
  14. ;
  15. Func _SoundOpen($hFile, $sAlias = "")
  16.     ;Declare variables
  17.     Local $sSnd_id, $iCurrentPos, $iRet, $asAlias
  18.     ;check for file
  19.     If Not FileExists($hFile) Then Return SetError(2, 0, 0)
  20.     ;search for whitespace by character
  21.     $asAlias = StringSplit($sAlias, "")
  22.     For $iCurrentPos = 1 To $asAlias[0]
  23.         If StringIsSpace($asAlias[$iCurrentPos]) Then Return SetError(3, 0, 0)
  24.     Next
  25.     ;create random alias if one is not supplied
  26.     If $sAlias = "" Then
  27.         $sSnd_id = RandomStr(10)
  28.     Else
  29.         $sSnd_id = $sAlias
  30.     EndIf
  31.     ;open file
  32.     $iRet = mciSendString("open " & FileGetShortName($hFile) & " alias " & FileGetShortName($sSnd_id))
  33.     Return SetError(0, $iRet, $sSnd_id)
  34. EndFunc   ;==>_SoundOpen
  35.  
  36. ;===============================================================================
  37. ;
  38. ; Function Name:   _SoundClose
  39. ; Description::    Closes a sound
  40. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen
  41. ; Requirement(s):  AutoIt 3.2 ++
  42. ; Return Value(s): 1 - Success, 0 and @error = 1 - Failure
  43. ; Author(s):       RazerM
  44. ;
  45. ;===============================================================================
  46. ;
  47. Func _SoundClose($sSnd_id)
  48.     If mciSendString("close " & FileGetShortName($sSnd_id)) = 0 Then
  49.         Return 1
  50.     Else
  51.         Return SetError(1, 0, 0)
  52.     EndIf
  53. EndFunc   ;==>_SoundClose
  54.  
  55. ;===============================================================================
  56. ;
  57. ; Function Name:   _SoundPlay
  58. ; Description::    Plays a sound from the current position (beginning is the default)
  59. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
  60. ;                   $fWait - If set to 1 the script will wait for the sound to finish before continuing
  61. ;                         - If set to 0 the script will continue while the sound is playing
  62. ; Requirement(s):  AutoIt 3.2 ++
  63. ; Return Value(s): 1 - Success, 0 - Failure
  64. ;                   @error = 2 - $fWait is invalid, @error = 1 - play failed
  65. ; Author(s):       RazerM
  66. ;
  67. ;===============================================================================
  68. ;
  69. Func _SoundPlay($sSnd_id, $fWait = 0)
  70.     ;Declare variables
  71.     Local $iRet
  72.     ;validate $fWait
  73.     If $fWait <> 0 And $fWait <> 1 Then Return SetError(2, 0, 0)
  74.     ;if sound has finished, seek to start
  75.     If _SoundPos($sSnd_id, 2) = _SoundLength($sSnd_id, 2) Then mciSendString("seek " & FileGetShortName($sSnd_id) & " to start")
  76.     ;If $fWait = 1 then pass wait to mci
  77.     If $fWait = 1 Then
  78.         $iRet = mciSendString("play " & FileGetShortName($sSnd_id) & " wait")
  79.     Else
  80.         $iRet = mciSendString("play " & FileGetShortName($sSnd_id))
  81.     EndIf
  82.     ;return
  83.     If $iRet = 0 Then
  84.         Return 1
  85.     Else
  86.         Return SetError(1, 0, 0)
  87.     EndIf
  88. EndFunc   ;==>_SoundPlay
  89.  
  90. ;===============================================================================
  91. ;
  92. ; Function Name: _SoundStop
  93. ; Description::    Stops the sound
  94. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
  95. ; Requirement(s):  AutoIt 3.2 ++
  96. ; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
  97. ; Author(s):       RazerM
  98. ;
  99. ;===============================================================================
  100. ;
  101. Func _SoundStop($sSnd_id)
  102.     ;Declare variables
  103.     Local $iRet, $iRet2
  104.     ;seek to start
  105.     $iRet = mciSendString("seek " & FileGetShortName($sSnd_id) & " to start")
  106.     ;stop
  107.     $iRet2 = mciSendString("stop " & FileGetShortName($sSnd_id))
  108.     ;return
  109.     If $iRet = 0 And $iRet2 = 0 Then
  110.         Return 1
  111.     Else
  112.         Return SetError(1, 0, 0)
  113.     EndIf
  114. EndFunc   ;==>_SoundStop
  115.  
  116. ;===============================================================================
  117. ;
  118. ; Function Name:   _SoundPause
  119. ; Description::    Pauses the sound
  120. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
  121. ; Requirement(s):  AutoIt 3.2 ++
  122. ; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
  123. ; Author(s):       RazerM
  124. ;
  125. ;===============================================================================
  126. ;
  127. Func _SoundPause($sSnd_id)
  128.     ;Declare variables
  129.     Local $iRet
  130.     ;pause sound
  131.     $iRet = mciSendString("pause " & FileGetShortName($sSnd_id))
  132.     ;return
  133.     If $iRet = 0 Then
  134.         Return 1
  135.     Else
  136.         Return SetError(1, 0, 0)
  137.     EndIf
  138. EndFunc   ;==>_SoundPause
  139.  
  140. ;===============================================================================
  141. ;
  142. ; Function Name:   _SoundResume
  143. ; Description::    Resumes the sound after being paused
  144. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
  145. ; Requirement(s):  AutoIt 3.2 ++
  146. ; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
  147. ; Author(s):       RazerM
  148. ;
  149. ;===============================================================================
  150. ;
  151. Func _SoundResume($sSnd_id)
  152.     ;Declare variables
  153.     Local $iRet
  154.     ;resume sound
  155.     $iRet = mciSendString("resume " & FileGetShortName($sSnd_id))
  156.     ;return
  157.     If $iRet = 0 Then
  158.         Return 1
  159.     Else
  160.         Return SetError(1, 0, 0)
  161.     EndIf
  162. EndFunc   ;==>_SoundResume
  163.  
  164. ;===============================================================================
  165. ;
  166. ; Function Name:   _SoundLength
  167. ; Description::    Returns the length of the sound in the format hh:mm:ss
  168. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file,
  169. ;                   $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds
  170. ; Requirement(s):  AutoIt 3.2 ++
  171. ; Return Value(s): Length of the sound - Success, 0 and @error = 1 - $iMode is invalid
  172. ; Author(s):       RazerM
  173. ;
  174. ;===============================================================================
  175. ;
  176. Func _SoundLength($sSnd_id, $iMode = 1)
  177.     ;Declare variables
  178.     Local $iSnd_len_ms, $iSnd_len_min, $iSnd_len_hour, $iSnd_len_sec, $sSnd_len_format
  179.     ;validate $iMode
  180.     If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0)
  181.     ;tell mci to use time in milliseconds
  182.     mciSendString("set " & FileGetShortName($sSnd_id) & " time format miliseconds")
  183.     ;recieve length of sound
  184.     $iSnd_len_ms = mciSendString("status " & FileGetShortName($sSnd_id) & " length")
  185.     ;assign modified data to variables
  186.     $iSnd_len_min = Int($iSnd_len_ms / 60000)
  187.     $iSnd_len_hour = Int($iSnd_len_min / 60)
  188.     $iSnd_len_sec = Int(Int($iSnd_len_ms / 1000) - ($iSnd_len_min * 60))
  189.     ;assign formatted data to $sSnd_len_format
  190.     $sSnd_len_format = StringFormat("%02i:%02i:%02i", $iSnd_len_hour, $iSnd_len_min, $iSnd_len_sec)
  191.     ;return correct variable
  192.     If $iMode = 1 Then Return $sSnd_len_format
  193.     If $iMode = 2 Then Return $iSnd_len_ms
  194. EndFunc   ;==>_SoundLength
  195.  
  196. ;===============================================================================
  197. ;
  198. ; Function Name:   _SoundSeek
  199. ; Description::    Seeks the sound to a specified time
  200. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen (must NOT be a file), $iHour, $iMin, $iSec
  201. ; Requirement(s):  AutoIt 3.2 ++
  202. ; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
  203. ; Author(s):       RazerM
  204. ;
  205. ;===============================================================================
  206. ;
  207. Func _SoundSeek($sSnd_id, $iHour, $iMin, $iSec)
  208.     ;Declare variables
  209.     Local $iMs = 0
  210.     Local $iRet
  211.     ;prepare mci to recieve time in milliseconds
  212.     mciSendString("set " & FileGetShortName($sSnd_id) & " time format miliseconds")
  213.     ;modify the $iHour, $iMin and $iSec parameters to be in milliseconds
  214.     ;and add to $iMs
  215.     $iMs += $iSec * 1000
  216.     $iMs += $iMin * 60 * 1000
  217.     $iMs += $iHour * 60 * 60 * 1000
  218.     ; seek sound to time ($iMs)
  219.     $iRet = mciSendString("seek " & FileGetShortName($sSnd_id) & " to " & $iMs)
  220.     ;return
  221.     If $iRet = 0 Then
  222.         Return 1
  223.     Else
  224.         Return SetError(1, 0, 0)
  225.     EndIf
  226. EndFunc   ;==>_SoundSeek
  227.  
  228. ;===============================================================================
  229. ;
  230. ; Function Name:   _SoundStatus
  231. ; Description::    All devices can return the "not ready", "paused", "playing", and "stopped" values.
  232. ;                   Some devices can return the additional "open", "parked", "recording", and "seeking" values.(MSDN)
  233. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
  234. ; Requirement(s):  AutoIt 3.2 ++
  235. ; Return Value(s): Sound Status
  236. ; Author(s):       RazerM
  237. ;
  238. ;===============================================================================
  239. ;
  240. Func _SoundStatus($sSnd_id)
  241.     ;return status
  242.     Return mciSendString("status " & FileGetShortName($sSnd_id) & " mode")
  243. EndFunc   ;==>_SoundStatus
  244.  
  245. ;===============================================================================
  246. ;
  247. ; Function Name:   _SoundPos
  248. ; Description::    Returns the current position of the song
  249. ; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file,
  250. ;                   $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds
  251. ; Requirement(s):  AutoIt 3.2 ++
  252. ; Return Value(s): Current Position - Success, @error = 1 - $iMode is invalid
  253. ; Author(s):       RazerM
  254. ;
  255. ;===============================================================================
  256. ;
  257. Func _SoundPos($sSnd_id, $iMode = 1)
  258.     ;Declare variables
  259.     Local $iSnd_pos_ms, $iSnd_pos_min, $iSnd_pos_hour, $iSnd_pos_sec, $sSnd_pos_format
  260.     ;validate $iMode
  261.     If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0)
  262.     ;tell mci to use time in milliseconds
  263.     mciSendString("set " & FileGetShortName($sSnd_id) & " time format miliseconds")
  264.     ;receive position of sound
  265.     $iSnd_pos_ms = mciSendString("status " & FileGetShortName($sSnd_id) & " position")
  266.     ;modify data and assign to variables
  267.     $iSnd_pos_min = Int($iSnd_pos_ms / 60000)
  268.     $iSnd_pos_hour = Int($iSnd_pos_min / 60)
  269.     $iSnd_pos_sec = Int(Int($iSnd_pos_ms / 1000) - ($iSnd_pos_min * 60))
  270.     ;assign formatted data to $sSnd_pos_format
  271.     $sSnd_pos_format = StringFormat("%02i:%02i:%02i", $iSnd_pos_hour, $iSnd_pos_min, $iSnd_pos_sec)
  272.     ;return correct variable
  273.     If $iMode = 1 Then Return $sSnd_pos_format
  274.     If $iMode = 2 Then Return $iSnd_pos_ms
  275. EndFunc   ;==>_SoundPos
  276.  
  277. ;internal functions
  278. Func mciSendString($string)
  279.     Local $iRet
  280.     $iRet = DllCall("winmm.dll", "int", "mciSendStringA", "str", $string, "str", "", "int", 65534, "hwnd", 0)
  281.     If Not @error Then Return $iRet[2]
  282. EndFunc   ;==>mciSendString
  283.  
  284. Func RandomStr($len)
  285.     Local $string
  286.     For $iCurrentPos = 1 To $len
  287.         $string &= Chr(Random(97, 122, 1))
  288.     Next
  289.     Return $string
  290. EndFunc   ;==>RandomStr